package vip.xiaonuo.common.util;

import java.util.ArrayList;

public class XMLDocument {
    private String iXMLString = "";

    public XMLDocument() {
    }

    public XMLDocument(String aXMLString) {
        this.iXMLString = aXMLString;
    }

    public String toString() {
        return this.iXMLString;
    }

    public XMLDocument getValue(String aTag) {
        XMLDocument tXMLDocument = null;
        int tStartIndex = this.iXMLString.indexOf("<" + aTag.trim() + ">");
        int tEndIndex = this.iXMLString.indexOf("</" + aTag.trim() + ">");
        if (tStartIndex >= 0 && tEndIndex >= 0 && tStartIndex < tEndIndex) {
            tXMLDocument = new XMLDocument(this.iXMLString.substring(tStartIndex + aTag.length() + 2, tEndIndex));
        }

        return tXMLDocument;
    }

    public String getValueNoNull(String aTag) {
        String tValue = "";
        XMLDocument tXML = this.getValue(aTag);
        if (tXML != null) {
            tValue = tXML.toString();
        }

        return tValue;
    }

    public ArrayList getDocuments(String aTag) {
        String tXMLString = this.iXMLString;
        ArrayList tValues = new ArrayList();

        while (true) {
            XMLDocument tXMLDocument = null;
            int tStartIndex = tXMLString.indexOf("<" + aTag.trim() + ">");
            int tEndIndex = tXMLString.indexOf("</" + aTag.trim() + ">");
            if (tStartIndex == -1 || tEndIndex == -1 || tStartIndex > tEndIndex) {
                return tValues;
            }

            tXMLDocument = new XMLDocument(tXMLString.substring(tStartIndex, tEndIndex + aTag.length() + 3));
            tValues.add(tXMLDocument);
            tXMLString = tXMLString.substring(tEndIndex + 1);
        }
    }

    public XMLDocument getFormatDocument(String aSpace) {
        return this.getFormatDocument(0, aSpace);
    }

    private XMLDocument getFormatDocument(int aLevel, String aSpace) {
        String tSpace1 = aSpace;

        for (int i = 0; i < aLevel; ++i) {
            tSpace1 = tSpace1 + aSpace;
        }

        String tTagName = this.getFirstTagName();
        if (tTagName == null) {
            return this;
        } else {
            String tXMLString = "\n";

            for (XMLDocument tXMLDocument = new XMLDocument(this.iXMLString); (tTagName = tXMLDocument.getFirstTagName()) != null; tXMLDocument = tXMLDocument.deleteFirstTagDocument()) {
                XMLDocument tTemp = tXMLDocument.getValue(tTagName);
                String tSpace = "";
                if (tTemp.getFirstTagName() != null) {
                    tSpace = tSpace1;
                }

                tXMLString = tXMLString + tSpace1 + "<" + tTagName + ">" + tTemp.getFormatDocument(aLevel + 1, aSpace) + tSpace + "</" + tTagName + ">\n";
            }

            return new XMLDocument(tXMLString);
        }
    }

    public String getFirstTagName() {
        String tTagName = null;
        int tStartIndex = this.iXMLString.indexOf(60);
        int tEndIndex = this.iXMLString.indexOf(62);
        if (tEndIndex > tStartIndex) {
            tTagName = this.iXMLString.substring(tStartIndex + 1, tEndIndex);
        }

        return tTagName;
    }

    public XMLDocument deleteFirstTagDocument() {
        String tTagName = this.getFirstTagName();
        int tStartIndex = this.iXMLString.indexOf("<" + tTagName + ">");
        int tEndIndex = this.iXMLString.indexOf("</" + tTagName + ">");
        if (tEndIndex > tStartIndex) {
            this.iXMLString = this.iXMLString.substring(tEndIndex + tTagName.length() + 3);
        }

        return this;
    }
}
